package com.smart.web.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smart.consts.InfoType;
import com.smart.dao.Page;
import com.smart.dao.PageRequest;
import com.smart.model.Info;
import com.smart.model.InfoCond;
import com.smart.model.SelectItem;
import com.smart.model.SelectItemCond;
import com.smart.model.SelectType;
import com.smart.model.SelectTypeCond;
import com.smart.util.StringUtil;

@Controller
public class InfoController extends BaseController{
	private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
	
	@RequestMapping(value = "admin/info/list")
	public String adminListInfo(PageRequest pageRequest, InfoCond cond, Model model) {
		cond.addOrderByDesc("updTime");
		Page<Info> page = infoService.findAll(pageRequest, cond);
		model.addAttribute("page", page);
		model.addAttribute("cond", cond);
		
		return "admin/info/list";
	}
	
	@RequestMapping(value = "info/list")
	public String listInfo(PageRequest pageRequest, InfoCond cond, Model model) {
		pageRequest.setPageSize(15);
		cond.addOrderByDesc("updTime");
		Page<Info> page = infoService.findAll(pageRequest, cond);
		
		model.addAttribute("page", page);
		model.addAttribute("cond", cond);
		
		SelectTypeCond typeCond = new SelectTypeCond();
		typeCond.setCode("info.type");
		SelectType type = selectTypeService.findOne(typeCond);
		SelectItemCond selectItemCond = new SelectItemCond();
		selectItemCond.setSelectTypeId(type.getId());
		selectItemCond.addOrderByDesc("sort");
		List<SelectItem> items = selectItemService.find(selectItemCond);
		
		model.addAttribute("infoTypes", items);
		handleSelectedMenu();
		return view("info/list");
	}
	
	@RequestMapping(value = "info/{type}")
	public String infoList(@PathVariable InfoType type, PageRequest pageRequest) {
		InfoCond cond = new InfoCond();
		cond.setType(type);
		cond.addOrderByDesc("crtTime");
		PageRequest top10PageReq = new PageRequest();
		top10PageReq.setPageSize(10);
		
		Page<Info> top10 = infoService.findAll(top10PageReq, cond);
		Page<Info> page = infoService.findAll(pageRequest, cond);
		super.addRequestAttribute("top10", top10);
		super.addRequestAttribute("page", page);
		super.addRequestAttribute("type", type);
		return "info/list";
	}
	
	@RequestMapping(value = "info/{type}/{pageNumber}.html")
	public String page(@PathVariable InfoType type, @PathVariable int pageNumber) {
		PageRequest pageRequest = new PageRequest();
		pageRequest.setPageNumber(pageNumber);
		
		return infoList(type, pageRequest);
	}
	
	@RequestMapping(value = "info/{id}.html")
	public String infoDetail(@PathVariable Long id) {
		super.addRequestAttribute("infoData", infoService.getDataForInfoView(id));
		infoService.updateViewCount(id);
		return "info/view";
	}
	
	@RequestMapping(value = "info/viewCount/{id}")
	public @ResponseBody String viewCount(@PathVariable Long id) {
		Info info = infoService.findOne(id);
		return "" + info.getViewCount();
	}
	
	@RequestMapping(value = "admin/info/edit", method=RequestMethod.GET)
	public String preEditInfo(InfoType selectedType, Model model, HttpServletRequest request, Long id) {
		if(id!=null){
			Info info = infoService.findOne(id);
			info.setCrtTimeStr2(FORMATTER.format(info.getCrtTime()));
			model.addAttribute("obj", info);
		}
		super.addRequestAttribute("selectedType", selectedType);
		return "admin/info/edit";
	}
	
	@RequestMapping(value = "admin/info/edit", method=RequestMethod.POST)
	public String editInfo(Info form, HttpServletRequest request, Model model, RedirectAttributes ra) {
		String time = form.getCrtTimeStr2();
		if(time!=null && !"".equals(time)) {
			Date d = null;
			try {
				d = FORMATTER.parse(time);
			} catch (ParseException e) {
				d = new Date();
			}
			form.setCrtTime(d);
		}
		
		infoService.save(form);
		request.setAttribute("obj", form);
		setSuccessMsg(ra);
		return "redirect:edit?id=" + form.getId();
	}
	
	@RequestMapping(value = "admin/info/delete")
	public @ResponseBody String delete(Long id) {
		infoService.delete(id);
		return "OK";
	}
	
	
	
	@RequestMapping(value = "viewInfo")
	public String listInfo(Long id, Model model) {
		Info info = infoService.findOne(id);
		model.addAttribute("info", info);
		return "viewInfo";
	}
	
	@RequestMapping("admin/info/findByName")
	public @ResponseBody List<Info> findByName(String title,Model model){
		InfoCond cond=new InfoCond();
		if(StringUtil.isNotEmpty(title)) {
			cond.setTitle("%"+title+"%");
		}
		List<Info> list=infoService.find(cond);
		return list;
	}

}
